# to stat user access for each domain

require "dbm"
require "00_common"


module PayloadExtractor
  def self.get_all_raw_files
    Common.get_all_files "./Jan_6"
  end

  def self.file_been_handled?(file_name)
    Common.file_exist?("./payloads", file_name)
  end

  def self.handle_each_file(file_name)
    puts "now handling #{file_name}...."
    if file_been_handled? file_name
      puts "---> file has been handled."
    else
      file = File.open "./Jan_6/" + file_name, 'r'
      new_file = File.open "payloads_" + file_name, 'w'
      begin
        while line = file.readline
          if line[0,1] != '#'
            fields = line.split('|')
            # LOCAL_TIME_STAMP    = 1
            # SESSION_ID          = 2
            # SESSION_START_TIME  = 3
            # CLIENT_IP           = 4
            # SERVER_IP           = 5
            # L4_PROTO            = 6
            # CLIENT_PORT         = 7
            # SERVER_PORT         = 8
            # CLIENT_PAYLOAD_HEX  = 9
            # SERVER_PAYLOAD_HEX  = 10
            # PROTOCOL            = 11
            # CLIENT_PAYLOAD      = 12
            # SERVER_PAYLOAD      = 13

            local_time = fields[LOCAL_TIME_STAMP].length == 0 ? "NULL" : fields[LOCAL_TIME_STAMP]
            server_ip = fields[SERVER_IP].length == 0 ? "NULL" : fields[SERVER_IP]
            l4_proto = fields[L4_PROTO].length == 0 ? "NULL" : fields[L4_PROTO]
            protocol = fields[PROTOCOL].length == 0 ? "NULL" : fields[PROTOCOL]
            cpayload = fields[CLIENT_PAYLOAD].length == 0 ? "NULL" : fields[CLIENT_PAYLOAD]
            spayload = fields[SERVER_PAYLOAD].length == 0 ? "NULL" : fields[SERVER_PAYLOAD]
                        
            new_line = local_time + "|" + server_ip  + "|" + l4_proto + "|" + protocol + "|" + cpayload  + "|" + spayload
            new_file.puts new_line
          end
        end
      rescue Exception => e
        puts "end of file"
        file.close
      end # of begin rescue

      new_file.close
      # to move the file
      Common.move_file("payloads_" + file_name, "./payloads")
    end
  end # of handle_each_file
end


begin
  all_files = PayloadExtractor.get_all_raw_files
  all_files.each do |file_name|
    PayloadExtractor.handle_each_file file_name 
  end
rescue Exception => e
  puts "ERROR:" + e.to_s   
end